<body>
<div class='header' style='height:76px;'><a href='index.php?action=home'></a></div>
<h3 class='title'>Convert Image</h3>
    
<?php
require_once('database/auth.php');
?>

<?php
include_once('templates/header.php');
include_once ('templates/nav.php');
?>

<?php
$fileName = $_FILES["uploaded_file"]["name"]; 
$fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"]; 
$fileType = $_FILES["uploaded_file"]["type"]; 
$fileSize = $_FILES["uploaded_file"]["size"]; 
$fileErrorMsg = $_FILES["uploaded_file"]["error"]; 
$fileName = preg_replace('#[^a-z.0-9]#i', '', $fileName);
$kaboom = explode(".", $fileName); 
$fileExt = end($kaboom); 


if (!$fileTmpLoc) {
    echo "ERROR: Please browse for a file before clicking the upload button.";
    exit();
} else if($fileSize > 2097152 ) { 
    echo "ERROR: Your file was larger than 2 Megabytes in size.";
    unlink($fileTmpLoc); 
    exit();
} else if (!preg_match("/.(gif|jpg|png)$/i", $fileName) ) {
     echo "ERROR: Your file was not .gif, .jpg, or .png.";
     unlink($fileTmpLoc);
     exit();
} else if ($fileErrorMsg == 1) { 
    echo "ERROR: An error occured while processing the file. Try again.";
    exit();
}

$moveResult = move_uploaded_file($fileTmpLoc, "uploads/$fileName");

if ($moveResult != true) {
    echo "ERROR: File not uploaded. Try again.";
    exit();
}

include_once("convert_func.php");

$target_file = "uploads/$fileName";
$resized_file = "uploads/resized_$fileName";
$wmax = 500;
$hmax = 500;
ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
$newcopy =  "uploads/resized_$fileName";

if (strtolower($fileExt) != "jpg") {
    $target_file = "uploads/resized_$fileName";
    $new_jpg = "uploads/resized_".$kaboom[0].".jpg";
    ak_img_convert_to_jpg($target_file, $new_jpg, $fileExt);
	$newfileExt = "$fileExt";
}
echo "<img title='Converted Image' src='$target_file'/><br /><br /><br />";
echo "Download Link:<a title='Download' href='$new_jpg'><strong>Converted Image</strong></a><br />";
echo "<a style='color:grey;font-size:12px;'>(Note! Right Click on the Link and select Save link as...)</a><br/><br/>";
echo "The file extension is <strong>$fileExt</strong> and was successfully converted to <strong>jpg</strong>.";

/*	
echo "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />";
echo "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
echo "It is an <strong>$fileType</strong> type of file.<br /><br />";
echo "The file extension is <strong>$fileExt</strong><br /><br />";
echo "The Error Message output for this upload is: $fileErrorMsg <br />";
*/

?>
<?php include ('templates/footer.php');?>

